/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.processor;

import lk.mrt.cse.wihidum.storage.Broker;
import lk.mrt.cse.wihidum.storage.BrokerList;
import lk.mrt.cse.wihidum.storage.ClientList;
import lk.mrt.cse.wihidum.util.WSEventingConstants;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.log4j.Logger;

import javax.xml.namespace.QName;
import java.util.ArrayList;

public class GetBrokerProcessor implements Processor
{
    private static final Logger logger = Logger.getLogger (BrokerList.class);

    public SOAPEnvelope processMessage(SOAPEnvelope requestEnvelope)
    {
        logger.info("Processing getBroker request");
        SOAPBody body=requestEnvelope.getBody();
        OMElement getBrokerElement=body.getFirstChildWithName(new QName(WSEventingConstants.WIHIDUM_NAMESPACE,WSEventingConstants.ElementNames.GetBroker));
        OMElement identifierElement=getBrokerElement.getFirstChildWithName(new QName(WSEventingConstants.WIHIDUM_NAMESPACE,WSEventingConstants.ElementNames.Identifier));
        String clientAddress=identifierElement.getText();
        ClientList.addClientToList(clientAddress);
        
        ArrayList<Broker> returningBrokerList=getBrokerSubList();

        SOAPFactory factory= OMAbstractFactory.getSOAP12Factory();
        SOAPEnvelope responseEnvelope=factory.getDefaultEnvelope();

        OMNamespace wihidumNS=factory.createOMNamespace(WSEventingConstants.WIHIDUM_NAMESPACE,WSEventingConstants.WHIDUM_NAMESPACE_PREFIX);
        responseEnvelope.declareNamespace(wihidumNS);

        OMElement responseElement=factory.createOMElement(WSEventingConstants.ElementNames.GetBrokerResponse,wihidumNS);
        OMElement brokerListElement=factory.createOMElement(WSEventingConstants.ElementNames.BrokerList,wihidumNS);
        brokerListElement.setText("List of Brokers");
        responseElement.addChild(brokerListElement);
        for(Broker broker:returningBrokerList)
        {
           logger.info("Broker in returning envelope:"+broker.getAddress());
           OMElement brokerElement=factory.createOMElement(WSEventingConstants.ElementNames.Broker,wihidumNS);
           brokerElement.setText(broker.getAddress());

           brokerListElement.addChild(brokerElement);

        }
        SOAPBody responseBody=responseEnvelope.getBody();
        responseBody.addChild(responseElement);
        return responseEnvelope;  //To change body of implemented methods use File | Settings | File Templates.
    }
    public ArrayList<Broker> getBrokerSubList()
    {
          ArrayList<Broker> returningBrokerList=BrokerList.shuffleAndReturnBrokerList();
          return returningBrokerList;
    }
    
   
}
